package cn.yunhe;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LiuYanDao extends BaseDB {
    int pageSize=3;
    Connection conn = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;

    public List<LiuYan> getAllNews(int pageIndex) {

        List<LiuYan> list = new ArrayList<>();
        try {
            conn = getConn();
            String sql = "select * from tb_liuyan order by createdate desc limit ?,?";
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1,pageSize*(pageIndex-1));
            psmt.setInt(2,pageSize);
            rs = psmt.executeQuery();
            while (rs.next()) {
                LiuYan liuYan = new LiuYan(rs.getInt("liu_id"), rs.getString("liu_title"), rs.getString("createdate"));
                list.add(liuYan);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, psmt, rs);
        }
        return list;
    }

    //总条数
    public int getCount(){
        String sql="select count(1) from tb_liuyan";
        return FenYe.boardCount(sql);
    }

    //总页数
    public int getPageNum(){
        return FenYe.pageSize(getCount(),pageSize);
    }
}
